#!/bin/bash
#
# lc_modprobe - add lustre module options into modprobe.conf or 
#               modules.conf
#
#################################################################################

# Get the library of functions
. @scriptlibdir@/lc_common

# Check the kernel version
KERNEL_VERSION=`uname -r`
KERNEL_VERSION=${KERNEL_VERSION:0:3}

if [ "${KERNEL_VERSION}" = "2.4" ]; then
	MODULE_CONF=/etc/modules.conf
else
	MODULE_CONF=/etc/modprobe.conf
fi

read -r NETWORKS
MODLINES_FILE=/tmp/modlines$$.txt
START_MARKER=$"# start lustre config"
END_MARKER=$"# end lustre config"

# Generate a temp file contains lnet options lines 
generate_lnet_lines() {
	local LNET_LINE TMP_LINE

	TMP_LINE="${NETWORKS}"

	echo ${START_MARKER} > ${MODLINES_FILE}
	echo "# Lustre module options added automatically by `basename $0`" >> ${MODLINES_FILE}
	while true; do
		LNET_LINE=${TMP_LINE%%\\n*}
        	echo ${LNET_LINE} >> ${MODLINES_FILE}

        	TMP_LINE=${TMP_LINE#*\\n}

		if [ "${TMP_LINE}" == "${LNET_LINE}" ]; then
                	break
        	fi
	done
	echo ${END_MARKER} >> ${MODLINES_FILE}

	#echo "--------------${MODLINES_FILE}--------------"
	#cat ${MODLINES_FILE}
	#echo -e "------------------------------------------\n"

	return 0
}

if ! generate_lnet_lines; then
	exit 1	
fi

MODULE_CONF=$(fcanon ${MODULE_CONF})
# Add lnet options lines to the module configuration file
if [ -e ${MODULE_CONF} ]; then
	# Delete the old options
	sed -i "/${START_MARKER}/,/${END_MARKER}/d" ${MODULE_CONF}
fi

cat ${MODLINES_FILE} >> ${MODULE_CONF}
rm -f ${MODLINES_FILE}
exit 0
